﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using EventBroker.Module;
using Mbs;
using Mbs.Windows;

namespace EventBroker.Views
{
    public partial class CustomerListView : FormView,ICustomerListView
    {
        public CustomerListView()
        {
            InitializeComponent();
        }

        public string CustomerId
        {
            get { return txtCustomerId.Text; }
            set { txtCustomerId.Text = value; }
        }

        public void CustomerIdFocus()
        {
            txtCustomerId.Focus();
        }

        public void Display()
        {
            ShowDialog();
        }

        public void DisplayGlobalCustomers(List<string> customers)
        {
            DisplayCustomerList(lstGlobalCustomers, customers);
        }

        public void DisplayLocalCustomers(List<string> customers)
        {
            DisplayCustomerList(lstLocalCustomers, customers);
        }

        public override IEnumerator<IDisposable> Attach()
        {
            yield return CommandNames.ProcessLocalCustomer.Attach(btnProcessLocal, "Click");
            yield return CommandNames.AddLocalCustomer.Attach(btnAddLocalCustomer, "Click");
            yield return CommandNames.AddGlobleCustomer.Attach(btnAddGlobalCustomer, "Click");
            yield return CommandNames.CanelProcessLocalCustomer.Attach(btnCancelProcess, "Click");
        }

        public override IEnumerator<IDisposable> Subscribe()
        {
            yield return CommandNames.GlobalCustomerAdded.Subscribe<List<string>>((s, ea) => DisplayGlobalCustomers(ea.Message));
        }

        void DisplayCustomerList(ListBox lst, List<string> customers)
        {
            lst.DataSource = null;
            lst.DataSource = customers;
        }
    }
}
